<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include_once "sales.juris.edit.page.inc";
function juris_list_page()
{
    @$output = drupal_render(drupal_get_form('juris_list_form'));
    return $output;
}


function juris_list_form($form, &$form_state)
{
    $form['juris'] = array(
        '#prefix' => '<div class="juris-fuzzy-search">',
        '#suffix' => '</div>',
        '#type' => 'textfield',
        '#default_value' => !empty($form_state['values']['fuzzy_juris']) ? $form_state['values']['fuzzy_juris'] : '',
        '#attributes' => array('placeholder' => '管辖名称'),
        '#validated' => TRUE,
    );
    $table['#markup'] =  data_list_view_page($form, $form_state);
    ajax_submit_form($form, $form_state, $table, '');
    return $form;
}

function juris_list_form_validate($form, &$form_state)
{
}

function data_list_view_page($form = '', &$form_state = '')
{
        $output = '';
        //表头
        $header = array(
            array('data' => '选择', 'class' => ['essential no']),
            array('data' => '管辖ID号', 'class' => ['essential juris_id'], 'field' => 'juris_id'),
            array('data' => '管辖名称', 'class' => ['essential name']),
            array('data' => '操作', 'class' => ['essential more']),
        );
        $query = db_select('juris', 'J')->extend('PagerDefault')->extend('TableSort');
        $query->fields('J');
        //模糊条件筛选
        if (!empty($form_state['values']['juris'])) {//模糊地址查询条件
            $query->condition('J.juris_name', '%' . db_like($form_state['values']['juris']) . '%', 'LIKE');
        }
        if($form_state['triggering_element']['#value'] != '查询')
            $query->orderBy('J.timestamp', 'DESC');
        $query->limit(variable_get("list_page_item_number", 5));

        $jurisInfo = $query->orderByHeader($header)->execute();

        $rows = array();
        //表内容
        $n = 1;
        foreach ($jurisInfo as $row) {
            $rows[] = array(
                array('data' => '<input type="checkbox" class="listViewTableCheck" value ="' . $row->juris_id . '"/>' . $n++, 'class' => ['essential no']),
                array('data' => $row->juris_id, 'class' => ['essential juris_id']),
                array('data' => $row->juris_name, 'class' => ['essential juris_name']),
                array('data' => '<a data-trigger="modal" href="'.base_path().'sales/juris/edit/' . $row->juris_id . '" data-title="修改" class="btn btn-mini">修改</a>', 'class' => 'essential edit'),
            );
        }
        $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('没有记录'), 'attributes' => array('class' => array('table table-bordered table-striped table-hover enhanced')))); //表格
        /*	 * *按钮** */
        if(sizeof($rows) > 0) {
            $output .= '<a href="javascript:void(0)" id="listViewTableSelectAll" data-toggle="modal" class="btn btn-mini">全选</a>';
            $output .= '<a href="javascript:void(0)" id="listViewTableDeleteSelected" data-toggle="modal" class="btn btn-mini">删除所选</a>';
        }
        $output .= theme('pager');
        return $output;
}

function juris_delete()
{
    ajax_table_delete('juris','juris_id');
}